Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 758)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.04100 13.03687 13.03281 13.02880 13.02485 13.02096 13.01712 13.01332
## [9] 13.00958 13.00588 13.00222 12.99861 12.99503 12.99149 12.98798 12.98450
## [17] 12.98104 12.97762 12.97421 12.97083 12.96747 12.96412 12.96078 12.95746
## [25] 12.95415 12.95084 12.94753 12.94423 12.94092 12.93761 12.93430 12.93098
## [33] 12.92764 12.92430 12.92093 12.91755 12.91415 12.91075 12.90733 12.90391
## [41] 12.90049 12.89707 12.89366 12.89026 12.88686 12.88349 12.88013 12.87680
## [49] 12.87349 12.87020 12.86696 12.86374 12.86057 12.85743 12.85435 12.85131
## [57] 12.84832 12.84539 12.84252 12.83970 12.83696 12.83428 12.83167 12.82914
## [65] 12.82669 12.82431 12.82203 12.81983 12.81772 12.81571 12.81379 12.81198
## [73] 12.81027 12.80867 12.80718 12.80581 12.80455 12.80341 12.80224 12.80088
## [81] 12.79934 12.79764 12.79579 12.79380 12.79169 12.78946 12.78713 12.78472
## [89] 12.78223 12.77968 12.77708 12.77444 12.77178 12.76911 12.76644 12.76379
## [97] 12.76116 12.75858 12.75604 12.75357 12.75118 12.74888 12.74668 12.74459
## [105] 12.74264 12.74083 12.73917 12.73767 12.73636 12.73524 12.73432 12.73362
## [113] 12.73315 12.73293 12.73296 12.73325 12.73383 12.73470 12.73588 12.73738
## [121] 12.73954 12.74266 12.74667 12.75151 12.75710 12.76338 12.77028 12.77773
## [129] 12.78568 12.79403 12.80275 12.81174 12.82095 12.83031 12.83975 12.84920
## [137] 12.85860 12.86788 12.87697 12.88580 12.89431 12.90243 12.91008 12.91722
## [145] 12.92375 12.92963 12.93478 12.94082 12.94925 12.95982 12.97227 12.98637
## [153] 13.00185 13.01847 13.03598 13.05412 13.07264 13.09130 13.10984 13.12801
## [161] 13.14556 13.16224 13.17780 13.19199 13.20455 13.21524 13.22381 13.23000
## [169] 13.23540 13.24170 13.24881 13.25664 13.26509 13.27408 13.28352 13.29331
## [177] 13.30336 13.31359 13.32389 13.33419 13.34439 13.35440 13.36412 13.37347
## [185] 13.38236 13.39069 13.39838 13.40533 13.41146 13.41666 13.42086 13.42396
## [193] 13.42587 13.42649 13.42574 13.42353 13.41976 13.41435 13.40708 13.39790
## [201] 13.38695 13.37437 13.36033 13.34495 13.32840 13.31081 13.29233 13.27311
## [209] 13.25330 13.23304 13.21247 13.19175 13.17102 13.15043 13.13012 13.11025
## [217] 13.09095 13.07238 13.05467 13.03553 13.01280 12.98684 12.95801 12.92669
## [225] 12.89323 12.85800 12.82137 12.78370 12.74536 12.70670 12.66811 12.62994
## [233] 12.59255 12.55632 12.52160 12.48877 12.45819 12.43021 12.40522 12.38357
## [241] 12.36358 12.34338 12.32303 12.30256 12.28205 12.26154 12.24109 12.22075
## [249] 12.20058 12.18062 12.16095 12.14160 12.12264 12.10412 12.08609 12.06861
## [257] 12.05174 12.03552 12.02001 12.00526 11.99134 11.97904 11.96900 11.96100
## [265] 11.95483 11.95028 11.94714 11.94520 11.94425 11.94408 11.94448 11.94523
## [273] 11.94613 11.94697 11.94753 11.94760 11.94698 11.94545 11.94280 11.93883
## [281] 11.93331 11.92604 11.91822 11.91113 11.90469 11.89882 11.89344 11.88849
## [289] 11.88387 11.87952 11.87535 11.87129 11.86726 11.86318 11.85897 11.85456
## [297] 11.84987 11.84483 11.83934 11.83334 11.82675 11.81950 11.81149 11.80234
## [305] 11.79180 11.78004 11.76720 11.75345 11.73895 11.72384 11.70829 11.69246
## [313] 11.67649 11.66056 11.64481 11.62940 11.61450 11.60025 11.58682 11.57435
## [321] 11.56302 11.55297 11.54437 11.53737 11.52993 11.52013 11.50825 11.49459
## [329] 11.47945 11.46311 11.44587 11.42802 11.40987 11.39169 11.37379 11.35646
## [337] 11.34000 11.32469 11.31083 11.29871 11.28864 11.28090 11.27578 11.27358
## [345] 11.27460 11.27798 11.28263 11.28848 11.29547 11.30353 11.31260 11.32260
## [353] 11.33347 11.34514 11.35756 11.37064 11.38433 11.39856 11.41326 11.42836
## [361] 11.44381 11.45953 11.47545 11.49152 11.50765 11.52380 11.54172 11.56305
## [369] 11.58752 11.61485 11.64479 11.67704 11.71135 11.74744 11.78504 11.82388
## [377] 11.86369 11.90419 11.94512 11.98621 12.02718 12.06776 12.10768 12.14666
## [385] 12.18445 12.22077 12.25533 12.28789 12.31815 12.34586 12.37074 12.39252
## [393] 12.41372 12.43687 12.46172 12.48802 12.51549 12.54390 12.57298 12.60247
## [401] 12.63212 12.66168 12.69088 12.71947 12.74719 12.77379 12.79901 12.82259
## [409] 12.84428 12.86382 12.88096 12.89543 12.90698 12.91608 12.92343 12.92917
## [417] 12.93338 12.93620 12.93772 12.93808 12.93736 12.93571 12.93321 12.92999
## [425] 12.92616 12.92183 12.91712 12.91214 12.90700 12.90181 12.89670 12.89176
## [433] 12.88711 12.88288 12.87916 12.87411 12.86601 12.85517 12.84190 12.82649
## [441] 12.80926 12.79052 12.77058 12.74973 12.72830 12.70658 12.68488 12.66351
## [449] 12.64279 12.62300 12.60447 12.58751 12.57241 12.55948 12.54904 12.53903
## [457] 12.52732 12.51411 12.49958 12.48392 12.46734 12.45001 12.43213 12.41389
## [465] 12.39548 12.37710 12.35893 12.34116 12.32400 12.30762 12.29222 12.27799
## [473] 12.26512 12.25381 12.24424 12.23661 12.22988 12.22296 12.21591 12.20878
## [481] 12.20165 12.19459 12.18765 12.18090 12.17441 12.16824 12.16246 12.15713
## [489] 12.15232 12.14808 12.14450 12.14163 12.13953 12.13828 12.13794 12.13857
## [497] 12.14023 12.14355 12.14896 12.15630 12.16538 12.17603 12.18806 12.20131
## [505] 12.21559 12.23072 12.24653 12.26284 12.27947 12.29624 12.31297 12.32950
## [513] 12.34563 12.36119 12.37601 12.38991 12.40270 12.41421 12.42426 12.43268
## [521] 12.44131 12.45202 12.46459 12.47883 12.49454 12.51152 12.52957 12.54850
## [529] 12.56809 12.58817 12.60852 12.62894 12.64925 12.66923 12.68870 12.70744
## [537] 12.72527 12.74198 12.75738 12.77126 12.78343 12.79369 12.80184 12.80768
## [545] 12.81101 12.81164 12.81046 12.80855 12.80593 12.80262 12.79864 12.79402
## [553] 12.78879 12.78298 12.77659 12.76966 12.76222 12.75429 12.74589 12.73704
## [561] 12.72778 12.71812 12.70810 12.69773 12.68704 12.67605 12.66480 12.65330
## [569] 12.64157 12.62781 12.61042 12.58977 12.56623 12.54017 12.51196 12.48197
## [577] 12.45057 12.41812 12.38499 12.35156 12.31820 12.28527 12.25313 12.22217
## [585] 12.19275 12.16524 12.14001 12.11742 12.09785 12.08167 12.06546 12.04584
## [593] 12.02321 11.99797 11.97053 11.94129 11.91065 11.87901 11.84679 11.81437
## [601] 11.78217 11.75058 11.72002 11.69088 11.66356 11.63848 11.61602 11.59660
## [609] 11.58062 11.56849 11.55916 11.55128 11.54477 11.53956 11.53556 11.53269
## [617] 11.53087 11.53002 11.53006 11.53091 11.53249 11.53472 11.53752 11.54081
## [625] 11.54451 11.54853 11.55280 11.55724 11.56177 11.56630 11.57076 11.57507
## [633] 11.58062 11.58870 11.59911 11.61160 11.62596 11.64195 11.65936 11.67796
## [641] 11.69753 11.71783 11.73864 11.75974 11.78091 11.80191 11.82253 11.84253
## [649] 11.86169 11.87979 11.89660 11.91189 11.92545 11.93870 11.95315 11.96871
## [657] 11.98527 12.00273 12.02100 12.03997 12.05954 12.07960 12.10007 12.12083
## [665] 12.14179 12.16284 12.18389 12.20483 12.22557 12.24599 12.26601 12.28551
## [673] 12.30440 12.32259 12.33995 12.35641 12.37184 12.38617 12.39927 12.41164
## [681] 12.42381 12.43579 12.44758 12.45919 12.47062 12.48186 12.49292 12.50380
## [689] 12.51451 12.52505 12.53542 12.54562 12.55565 12.56552 12.57523 12.58478
## [697] 12.59417 12.60341 12.61249 12.62143 12.63022 12.63885 12.64733 12.65565
## [705] 12.66381 12.67180 12.67964 12.68731 12.69482 12.70216 12.70933 12.71634
## [713] 12.72318 12.72985 12.73635 12.74268 12.74883 12.75481 12.76062 12.76625
## [721] 12.77171 12.77700 12.78212 12.78708 12.79187 12.79649 12.80095 12.80524
## [729] 12.80937 12.81332 12.81711 12.82072 12.82417 12.82744 12.83054 12.83346
## [737] 12.83621 12.83879 12.84119 12.84342 12.84547 12.84734 12.84903 12.85053
## [745] 12.85186 12.85300 12.85397 12.85476 12.85537 12.85580 12.85606 12.85615
## [753] 12.85606 12.85580 12.85537 12.85477 12.85401 12.85307
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 758)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63127 12.62640 12.62163 12.61696 12.61239 12.60791 12.60353 12.59924
## [9] 12.59505 12.59094 12.58693 12.58300 12.57916 12.57541 12.57174 12.56816
## [17] 12.56465 12.56123 12.55789 12.55462 12.55143 12.54832 12.54528 12.54231
## [25] 12.53941 12.53659 12.53383 12.53114 12.52852 12.52596 12.52346 12.52103
## [33] 12.51866 12.51635 12.51409 12.51190 12.50975 12.50767 12.50564 12.50367
## [41] 12.50176 12.49992 12.49814 12.49643 12.49479 12.49322 12.49172 12.49030
## [49] 12.48896 12.48769 12.48651 12.48541 12.48440 12.48348 12.48264 12.48189
## [57] 12.48124 12.48069 12.48023 12.47987 12.47961 12.47946 12.47941 12.47947
## [65] 12.47964 12.47992 12.48032 12.48083 12.48145 12.48220 12.48307 12.48406
## [73] 12.48518 12.48642 12.48780 12.48930 12.49094 12.49272 12.49464 12.49674
## [81] 12.49899 12.50140 12.50397 12.50669 12.50955 12.51256 12.51570 12.51898
## [89] 12.52239 12.52592 12.52958 12.53336 12.53725 12.54125 12.54536 12.54958
## [97] 12.55389 12.55830 12.56280 12.56738 12.57206 12.57681 12.58163 12.58653
## [105] 12.59150 12.59653 12.60162 12.60676 12.61196 12.61721 12.62250 12.62784
## [113] 12.63321 12.63861 12.64404 12.64950 12.65498 12.66047 12.66598 12.67150
## [121] 12.67727 12.68352 12.69020 12.69727 12.70466 12.71234 12.72025 12.72835
## [129] 12.73660 12.74493 12.75331 12.76169 12.77001 12.77824 12.78632 12.79420
## [137] 12.80184 12.80919 12.81619 12.82281 12.82900 12.83470 12.84143 12.85057
## [145] 12.86187 12.87508 12.88995 12.90623 12.92367 12.94202 12.96104 12.98047
## [153] 13.00007 13.01958 13.03876 13.05735 13.07512 13.09180 13.10714 13.12091
## [161] 13.13285 13.14271 13.15024 13.15695 13.16447 13.17273 13.18163 13.19111
## [169] 13.20108 13.21146 13.22218 13.23315 13.24430 13.25555 13.26681 13.27802
## [177] 13.28908 13.29992 13.31046 13.32063 13.33033 13.33950 13.34805 13.35591
## [185] 13.36300 13.36923 13.37452 13.37881 13.38200 13.38402 13.38480 13.38424
## [193] 13.38228 13.37882 13.37381 13.36714 13.35798 13.34571 13.33062 13.31299
## [201] 13.29310 13.27123 13.24767 13.22270 13.19660 13.16966 13.14215 13.11436
## [209] 13.08658 13.05908 13.03215 13.00606 12.98112 12.95758 12.93575 12.91590
## [217] 12.89831 12.88037 12.85950 12.83600 12.81020 12.78239 12.75289 12.72201
## [225] 12.69007 12.65737 12.62423 12.59096 12.55787 12.52526 12.49347 12.46278
## [233] 12.43353 12.40601 12.38054 12.35743 12.33699 12.31954 12.30376 12.28813
## [241] 12.27267 12.25739 12.24232 12.22746 12.21284 12.19846 12.18434 12.17050
## [249] 12.15695 12.14371 12.13079 12.11820 12.10597 12.09411 12.08263 12.07155
## [257] 12.06089 12.05065 12.04085 12.03271 12.02723 12.02416 12.02325 12.02423
## [265] 12.02685 12.03085 12.03597 12.04196 12.04855 12.05549 12.06251 12.06937
## [273] 12.07580 12.08155 12.08636 12.08996 12.09211 12.09254 12.09100 12.08723
## [281] 12.08291 12.07983 12.07784 12.07680 12.07658 12.07703 12.07802 12.07940
## [289] 12.08103 12.08279 12.08452 12.08609 12.08736 12.08818 12.08843 12.08796
## [297] 12.08663 12.08430 12.08083 12.07609 12.06993 12.06195 12.05198 12.04021
## [305] 12.02686 12.01211 11.99617 11.97925 11.96153 11.94324 11.92455 11.90569
## [313] 11.88684 11.86821 11.84999 11.83240 11.81564 11.79989 11.78537 11.77228
## [321] 11.76081 11.75117 11.74098 11.72796 11.71244 11.69476 11.67525 11.65427
## [329] 11.63214 11.60919 11.58578 11.56224 11.53890 11.51610 11.49418 11.47348
## [337] 11.45434 11.43709 11.42207 11.40962 11.40008 11.39378 11.39107 11.39075
## [345] 11.39138 11.39294 11.39538 11.39868 11.40281 11.40772 11.41340 11.41980
## [353] 11.42689 11.43464 11.44302 11.45198 11.46151 11.47157 11.48212 11.49314
## [361] 11.50458 11.51642 11.52862 11.54115 11.55561 11.57344 11.59433 11.61800
## [369] 11.64415 11.67250 11.70274 11.73458 11.76774 11.80192 11.83682 11.87216
## [377] 11.90764 11.94297 11.97786 12.01201 12.04513 12.07692 12.10710 12.13538
## [385] 12.16145 12.18503 12.20583 12.22640 12.24934 12.27435 12.30117 12.32951
## [393] 12.35911 12.38968 12.42096 12.45265 12.48449 12.51620 12.54750 12.57812
## [401] 12.60778 12.63620 12.66311 12.68823 12.71128 12.73199 12.75009 12.76529
## [409] 12.77910 12.79317 12.80739 12.82168 12.83596 12.85012 12.86408 12.87776
## [417] 12.89105 12.90388 12.91614 12.92776 12.93864 12.94868 12.95781 12.96593
## [425] 12.97296 12.97879 12.98334 12.98653 12.98826 12.98774 12.98443 12.97857
## [433] 12.97038 12.96012 12.94803 12.93435 12.91932 12.90317 12.88617 12.86853
## [441] 12.85051 12.83235 12.81429 12.79657 12.77944 12.76312 12.74787 12.73393
## [449] 12.72154 12.71093 12.69973 12.68557 12.66879 12.64970 12.62862 12.60587
## [457] 12.58177 12.55663 12.53079 12.50455 12.47824 12.45218 12.42668 12.40207
## [465] 12.37866 12.35677 12.33673 12.31884 12.30344 12.29084 12.28136 12.27288
## [473] 12.26319 12.25246 12.24086 12.22856 12.21573 12.20254 12.18916 12.17576
## [481] 12.16251 12.14959 12.13716 12.12539 12.11445 12.10452 12.09575 12.08834
## [489] 12.08243 12.07821 12.07584 12.07550 12.07712 12.08044 12.08534 12.09173
## [497] 12.09948 12.10849 12.11864 12.12982 12.14193 12.15484 12.16845 12.18265
## [505] 12.19732 12.21236 12.22765 12.24307 12.25853 12.27390 12.28908 12.30395
## [513] 12.31840 12.33233 12.34561 12.35814 12.36981 12.38050 12.39304 12.41002
## [521] 12.43094 12.45534 12.48272 12.51261 12.54452 12.57798 12.61250 12.64761
## [529] 12.68281 12.71763 12.75158 12.78420 12.81498 12.84346 12.86915 12.89157
## [537] 12.91024 12.92468 12.93440 12.94165 12.94897 12.95629 12.96358 12.97077
## [545] 12.97781 12.98465 12.99124 12.99753 13.00346 13.00899 13.01405 13.01859
## [553] 13.02258 13.02594 13.02863 13.03060 13.03179 13.03216 13.03164 13.03020
## [561] 13.02776 13.02429 13.01973 13.01403 13.00713 12.99899 12.98955 12.97741
## [569] 12.96146 12.94206 12.91958 12.89439 12.86685 12.83733 12.80620 12.77381
## [577] 12.74054 12.70676 12.67282 12.63910 12.60596 12.57376 12.54288 12.51368
## [585] 12.48653 12.46179 12.43982 12.42100 12.40182 12.37880 12.35236 12.32292
## [593] 12.29088 12.25667 12.22070 12.18338 12.14513 12.10637 12.06750 12.02895
## [601] 11.99113 11.95444 11.91932 11.88617 11.85541 11.82745 11.80271 11.78160
## [609] 11.76454 11.74940 11.73385 11.71801 11.70199 11.68589 11.66984 11.65395
## [617] 11.63831 11.62306 11.60830 11.59414 11.58070 11.56808 11.55640 11.54578
## [625] 11.53632 11.52813 11.52133 11.51603 11.51234 11.51038 11.51070 11.51365
## [633] 11.51903 11.52661 11.53620 11.54756 11.56051 11.57481 11.59027 11.60667
## [641] 11.62380 11.64144 11.65939 11.67743 11.69536 11.71296 11.73001 11.74632
## [649] 11.76166 11.77582 11.78860 11.80206 11.81822 11.83683 11.85761 11.88031
## [657] 11.90467 11.93041 11.95728 11.98501 12.01333 12.04199 12.07072 12.09925
## [665] 12.12732 12.15467 12.18104 12.20615 12.22976 12.25158 12.27136 12.28884
## [673] 12.30524 12.32194 12.33888 12.35604 12.37336 12.39080 12.40832 12.42588
## [681] 12.44344 12.46094 12.47834 12.49562 12.51271 12.52958 12.54618 12.56248
## [689] 12.57842 12.59397 12.60908 12.62371 12.63781 12.65135 12.66427 12.67685
## [697] 12.68936 12.70180 12.71414 12.72638 12.73850 12.75049 12.76233 12.77402
## [705] 12.78553 12.79686 12.80800 12.81892 12.82961 12.84007 12.85028 12.86022
## [713] 12.86989 12.87926 12.88833 12.89708 12.90559 12.91391 12.92206 12.93003
## [721] 12.93782 12.94541 12.95282 12.96004 12.96705 12.97387 12.98049 12.98690
## [729] 12.99311 12.99910 13.00488 13.01044 13.01578 13.02090 13.02580 13.03046
## [737] 13.03489 13.03909 13.04305 13.04678 13.05027 13.05354 13.05659 13.05941
## [745] 13.06200 13.06438 13.06655 13.06849 13.07023 13.07175 13.07307 13.07418
## [753] 13.07509 13.07580 13.07631 13.07663 13.07675 13.07668
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 758)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.04338 12.03740 12.03152 12.02574 12.02006 12.01447 12.00898 12.00357
## [9] 11.99825 11.99301 11.98785 11.98277 11.97777 11.97285 11.96799 11.96321
## [17] 11.95849 11.95384 11.94925 11.94471 11.94024 11.93582 11.93145 11.92713
## [25] 11.92285 11.91863 11.91444 11.91029 11.90618 11.90211 11.89806 11.89405
## [33] 11.89006 11.88610 11.88216 11.87824 11.87434 11.87045 11.86658 11.86271
## [41] 11.85885 11.85500 11.85114 11.84730 11.84348 11.83968 11.83590 11.83216
## [49] 11.82846 11.82479 11.82117 11.81759 11.81407 11.81061 11.80720 11.80386
## [57] 11.80059 11.79739 11.79427 11.79123 11.78828 11.78542 11.78265 11.77998
## [65] 11.77741 11.77494 11.77259 11.77036 11.76824 11.76624 11.76437 11.76264
## [73] 11.76103 11.75957 11.75826 11.75709 11.75607 11.75521 11.75451 11.75397
## [81] 11.75360 11.75341 11.75339 11.75356 11.75391 11.75430 11.75462 11.75486
## [89] 11.75505 11.75519 11.75530 11.75539 11.75547 11.75555 11.75565 11.75578
## [97] 11.75595 11.75618 11.75647 11.75684 11.75731 11.75787 11.75856 11.75937
## [105] 11.76033 11.76144 11.76271 11.76417 11.76582 11.76767 11.76973 11.77203
## [113] 11.77457 11.77736 11.78042 11.78376 11.78738 11.79132 11.79557 11.80014
## [121] 11.80506 11.81034 11.81598 11.82199 11.82840 11.83522 11.84323 11.85312
## [129] 11.86471 11.87784 11.89235 11.90806 11.92480 11.94241 11.96072 11.97956
## [137] 11.99877 12.01817 12.03760 12.05689 12.07587 12.09438 12.11224 12.12929
## [145] 12.14536 12.16028 12.17389 12.18867 12.20698 12.22845 12.25271 12.27938
## [153] 12.30810 12.33850 12.37021 12.40284 12.43604 12.46944 12.50265 12.53532
## [161] 12.56706 12.59752 12.62631 12.65307 12.67743 12.69901 12.71745 12.73237
## [169] 12.74570 12.75956 12.77385 12.78850 12.80342 12.81853 12.83375 12.84898
## [177] 12.86416 12.87919 12.89398 12.90847 12.92256 12.93617 12.94921 12.96161
## [185] 12.97328 12.98413 12.99409 13.00306 13.01097 13.01773 13.02325 13.02746
## [193] 13.03027 13.03160 13.03136 13.02947 13.02585 13.02040 13.01219 13.00049
## [201] 12.98563 12.96792 12.94766 12.92515 12.90072 12.87467 12.84730 12.81893
## [209] 12.78986 12.76040 12.73087 12.70156 12.67280 12.64488 12.61812 12.59282
## [217] 12.56930 12.54786 12.52881 12.50926 12.48632 12.46036 12.43173 12.40078
## [225] 12.36787 12.33335 12.29758 12.26090 12.22368 12.18627 12.14901 12.11227
## [233] 12.07640 12.04175 12.00868 11.97753 11.94867 11.92245 11.89922 11.87934
## [241] 11.86153 11.84428 11.82754 11.81129 11.79551 11.78015 11.76520 11.75062
## [249] 11.73638 11.72245 11.70880 11.69540 11.68223 11.66925 11.65643 11.64374
## [257] 11.63115 11.61864 11.60617 11.59371 11.58124 11.56981 11.56039 11.55279
## [265] 11.54681 11.54228 11.53899 11.53676 11.53540 11.53473 11.53454 11.53466
## [273] 11.53488 11.53503 11.53492 11.53434 11.53312 11.53106 11.52798 11.52368
## [281] 11.51798 11.51068 11.50290 11.49582 11.48938 11.48352 11.47816 11.47325
## [289] 11.46871 11.46448 11.46050 11.45669 11.45300 11.44936 11.44570 11.44195
## [297] 11.43805 11.43394 11.42955 11.42480 11.41965 11.41402 11.40784 11.40017
## [305] 11.39028 11.37840 11.36477 11.34965 11.33327 11.31588 11.29771 11.27902
## [313] 11.26004 11.24102 11.22219 11.20381 11.18611 11.16934 11.15374 11.13955
## [321] 11.12702 11.11638 11.10788 11.10177 11.09612 11.08898 11.08057 11.07107
## [329] 11.06071 11.04969 11.03820 11.02647 11.01469 11.00306 10.99180 10.98111
## [337] 10.97120 10.96226 10.95451 10.94816 10.94340 10.94044 10.93950 10.94076
## [345] 10.94445 10.95061 10.95906 10.96961 10.98207 10.99627 11.01203 11.02916
## [353] 11.04748 11.06681 11.08697 11.10778 11.12905 11.15060 11.17226 11.19383
## [361] 11.21514 11.23601 11.25626 11.27570 11.29415 11.31142 11.32931 11.34958
## [369] 11.37203 11.39647 11.42271 11.45056 11.47981 11.51028 11.54178 11.57410
## [377] 11.60706 11.64046 11.67411 11.70782 11.74138 11.77461 11.80732 11.83930
## [385] 11.87037 11.90033 11.92899 11.95616 11.98163 12.00523 12.02674 12.04599
## [393] 12.06461 12.08428 12.10486 12.12617 12.14808 12.17043 12.19306 12.21582
## [401] 12.23856 12.26113 12.28337 12.30513 12.32625 12.34659 12.36599 12.38429
## [409] 12.40134 12.41700 12.43110 12.44349 12.45403 12.46320 12.47163 12.47934
## [417] 12.48636 12.49272 12.49845 12.50357 12.50812 12.51211 12.51558 12.51856
## [425] 12.52106 12.52313 12.52479 12.52606 12.52697 12.52756 12.52785 12.52786
## [433] 12.52762 12.52717 12.52652 12.52464 12.52058 12.51457 12.50679 12.49745
## [441] 12.48677 12.47494 12.46217 12.44867 12.43463 12.42027 12.40578 12.39138
## [449] 12.37727 12.36365 12.35073 12.33871 12.32780 12.31821 12.31013 12.30200
## [457] 12.29226 12.28105 12.26857 12.25497 12.24043 12.22511 12.20920 12.19285
## [465] 12.17625 12.15955 12.14293 12.12657 12.11063 12.09528 12.08069 12.06704
## [473] 12.05449 12.04322 12.03340 12.02519 12.01721 12.00809 11.99797 11.98702
## [481] 11.97540 11.96326 11.95077 11.93807 11.92534 11.91273 11.90039 11.88849
## [489] 11.87719 11.86665 11.85702 11.84846 11.84114 11.83521 11.83082 11.82815
## [497] 11.82734 11.82812 11.83006 11.83309 11.83711 11.84207 11.84789 11.85448
## [505] 11.86177 11.86970 11.87817 11.88712 11.89647 11.90614 11.91607 11.92616
## [513] 11.93635 11.94657 11.95673 11.96676 11.97658 11.98613 11.99531 12.00407
## [521] 12.01397 12.02649 12.04141 12.05849 12.07751 12.09823 12.12043 12.14387
## [529] 12.16833 12.19357 12.21936 12.24548 12.27169 12.29777 12.32348 12.34860
## [537] 12.37289 12.39613 12.41808 12.43851 12.45720 12.47391 12.48842 12.50049
## [545] 12.50990 12.51641 12.52154 12.52692 12.53250 12.53819 12.54396 12.54973
## [553] 12.55544 12.56102 12.56643 12.57160 12.57645 12.58094 12.58501 12.58858
## [561] 12.59160 12.59400 12.59573 12.59671 12.59690 12.59623 12.59463 12.59205
## [569] 12.58842 12.58286 12.57467 12.56410 12.55140 12.53682 12.52058 12.50295
## [577] 12.48415 12.46445 12.44407 12.42327 12.40229 12.38138 12.36077 12.34071
## [585] 12.32145 12.30323 12.28630 12.27089 12.25726 12.24564 12.23308 12.21672
## [593] 12.19700 12.17432 12.14912 12.12181 12.09281 12.06255 12.03144 11.99991
## [601] 11.96838 11.93727 11.90700 11.87799 11.85067 11.82544 11.80275 11.78300
## [609] 11.76661 11.75402 11.74371 11.73391 11.72462 11.71584 11.70757 11.69981
## [617] 11.69255 11.68580 11.67956 11.67382 11.66859 11.66387 11.65965 11.65593
## [625] 11.65273 11.65002 11.64782 11.64613 11.64494 11.64425 11.64407 11.64439
## [633] 11.64611 11.64999 11.65586 11.66350 11.67275 11.68340 11.69527 11.70816
## [641] 11.72188 11.73625 11.75108 11.76616 11.78133 11.79637 11.81111 11.82535
## [649] 11.83890 11.85157 11.86318 11.87353 11.88242 11.89112 11.90092 11.91175
## [657] 11.92350 11.93609 11.94941 11.96339 11.97793 11.99293 12.00831 12.02396
## [665] 12.03981 12.05576 12.07171 12.08757 12.10326 12.11868 12.13373 12.14833
## [673] 12.16238 12.17579 12.18847 12.20033 12.21128 12.22121 12.23005 12.23825
## [681] 12.24636 12.25435 12.26223 12.26998 12.27761 12.28511 12.29247 12.29969
## [689] 12.30676 12.31368 12.32044 12.32703 12.33345 12.33969 12.34576 12.35164
## [697] 12.35732 12.36281 12.36809 12.37316 12.37802 12.38271 12.38729 12.39176
## [705] 12.39610 12.40031 12.40438 12.40832 12.41211 12.41576 12.41924 12.42257
## [713] 12.42572 12.42871 12.43152 12.43415 12.43658 12.43883 12.44088 12.44272
## [721] 12.44435 12.44580 12.44709 12.44822 12.44919 12.45000 12.45065 12.45113
## [729] 12.45145 12.45161 12.45160 12.45142 12.45107 12.45056 12.44987 12.44901
## [737] 12.44798 12.44677 12.44539 12.44384 12.44210 12.44019 12.43810 12.43583
## [745] 12.43338 12.43075 12.42795 12.42497 12.42182 12.41850 12.41501 12.41135
## [753] 12.40752 12.40353 12.39937 12.39505 12.39057 12.38593
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")